//
//  DYZCardCityController.m
//  DYZCars
//
//  Created by xmy on 16/8/20.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZCardCityController.h"
#import "DSHTTPClient.h"
#import "YYModel.h"
#import "DYZChooseSeriesModel.h"
#import "DYZChooseSeriesChildModel.h"
#import "Masonry.h"
#import "UIColor+Addition.h"
#import "DYZChooseCarModelController.h"
#import "DYZRandomColorController.h"
#import "UILabel+Addition.h"
#import "AIMTableViewIndexBar.h"

static NSString *cellId = @"cellId";

@interface DYZCardCityController ()<UITableViewDelegate,UITableViewDataSource,AIMTableViewIndexBarDelegate>
@property(strong,nonatomic)NSDictionary *cityDict;
@property(strong,nonatomic)NSArray *cityKeys;
@property(weak,nonatomic)UITableView *tableView;
@property(weak,nonatomic)AIMTableViewIndexBar *indexBar;
@property(strong,nonatomic)NSArray *sections;

@end

@implementation DYZCardCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self addChoose];
    [self leftButton];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"上牌城市";
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.cityDict = [self loadPlist];
    
    //得到字典中所有的key,并按从A到Z进行排列
    self.cityKeys = [[self.cityDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
}
- (void)setupUI{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width - 20, self.view.bounds.size.height - 50) style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    self.tableView = tableView;
    //注册
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    
    //添加索引样式
    AIMTableViewIndexBar *indexBar = [[AIMTableViewIndexBar alloc]initWithFrame:CGRectMake(self.view.bounds.size.width - 20, 64, 20, 617)];
    self.indexBar = indexBar;
    
    self.indexBar.delegate = self;
    
    [self.view addSubview:self.indexBar];
    
    
}
//左侧返回按钮
- (void)leftButton{
    //左侧返回按钮
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    // 为返回按钮设置图片样式
    [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    //按钮的点击事件
    [button addTarget:self action:@selector(popController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = backButton;
    
    //将返回按钮的文字position设置不在屏幕上显示
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
}
//左侧按钮点击事件
- (void)popController{
    
    [self.navigationController popViewControllerAnimated:YES];
}

//添加自定义view
- (void)addChoose{
    
    UIView *addView = [[UIView alloc]init];
    addView.userInteractionEnabled = YES;
    
    //背景图片
    UIImageView *backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"more2"]];
    backImage.bounds = addView.frame;
    [addView addSubview:backImage];
    
    UILabel *label = [UILabel labelWithText:@"自定义区域" andTextColor:[UIColor whiteColor] andFontSize:14];
    [label sizeToFit];
    [addView addSubview:label];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow2"]];
    
    [addView addSubview:imageView];
    [imageView sizeToFit];
    
    [self.view addSubview:addView];
    
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    
    [addView addGestureRecognizer:tap];
    
    [addView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.offset(50);
    }];
    [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(addView);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.bottom.equalTo(addView);
    }];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.centerY.equalTo(addView);
    }];
    
    
}
//手势事件
- (void)tapGesture:(UITapGestureRecognizer *)tap{
    
    DYZRandomColorController *vc = [[DYZRandomColorController alloc]init];
    
    vc.flag = 2;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

//解析数据
- (NSDictionary *)loadPlist{
    NSURL *path = [[NSBundle mainBundle]URLForResource:@"cityList.plist"withExtension:nil];
    NSDictionary *cityDict = [NSDictionary dictionaryWithContentsOfURL:path];
    
    return cityDict;
}
////设置组索引
//-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//    
//    //设置组索引的文字样式
//
//    self.tableView.sectionIndexColor = [UIColor colorWithHex:0x666666];
//    
//    return self.cityKeys;
//    
//}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    [self.indexBar setIndexes:self.cityKeys];
    return self.cityKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //获取每个字典里的key
    NSString *str = self.cityKeys[section];
    //通过key可以找到对应数组
    NSArray *arr1 = self.cityDict[str];
    //获取字典里数组的个数
    return arr1.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    //获取每个字典里的key
    NSString *str = self.cityKeys[indexPath.section];
    //通过key可以找到对应数组
    NSArray *arr1 = self.cityDict[str];
    
    cell.textLabel.text = arr1[indexPath.row];
    
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    [cell.textLabel setTextColor:[UIColor colorWithHex:0x333333]];
    
    return cell;

}

//设置组头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.cityKeys[section];
}


//组头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc]init];
    [header setBackgroundColor:[UIColor colorWithHex:0xf0f0f0]];
    CGRect re = header.frame;
    re.size.height = 30;
    
    UILabel *label = [[UILabel alloc]init];
    
    label.textColor = [UIColor colorWithHex:0x666666];
    [label sizeToFit];
    label.font = [UIFont systemFontOfSize:12];
    [header addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(header);
        make.left.offset(10);
    }];
    label.text = self.cityKeys[section];
    return header;
    
}


#pragma mark - AIMTableViewIndexBarDelegate

- (void)tableViewIndexBar:(AIMTableViewIndexBar *)indexBar didSelectSectionAtIndex:(NSInteger)index{
    if ([self.tableView numberOfSections] > index && index > -1){   // for safety, should always be YES
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]
                              atScrollPosition:UITableViewScrollPositionTop
                                      animated:YES];
    }
}


//cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}

@end
